/**
 * @description 服务索引文件
 * @author AI Assistant
 * @date 2023-10-27
 */

import { ListService } from './list'
import { GroupService } from './group'
import { TaskService } from './task'
import { NoteService } from './note'

// 创建服务实例
export const listService = new ListService()
export const groupService = new GroupService()
export const taskService = new TaskService()
export const noteService = new NoteService()

// 手动创建TimerRecordService实例
import { BaseService } from './base'
import { STORAGE_KEYS } from '@/utils/storage'
import { TimerRecord } from '@/models/timerRecord'

class TimerRecordService extends BaseService {
  constructor() {
    super(STORAGE_KEYS.TIMER_RECORDS)
  }

  create(data) {
    const record = new TimerRecord(data)
    return super.create(record.toJSON())
  }

  // 根据任务ID获取计时记录
  getRecordsByTaskId(taskId) {
    const records = this.getAll()
    return records.filter(record => record.taskId === taskId)
      .sort((a, b) => b.startTime - a.startTime) // 按开始时间降序排序
  }

  // 获取指定日期范围内的记录
  getRecordsByDateRange(startDate, endDate) {
    const records = this.getAll()
    return records.filter(record => {
      return record.startTime >= startDate && record.startTime <= endDate
    }).sort((a, b) => a.startTime - b.startTime)
  }

  // 获取今日记录
  getTodayRecords() {
    const today = new Date()
    const startOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime()
    const endOfDay = startOfDay + 24 * 60 * 60 * 1000 - 1
    return this.getRecordsByDateRange(startOfDay, endOfDay)
  }

  // 获取本周记录
  getThisWeekRecords() {
    const today = new Date()
    const dayOfWeek = today.getDay() || 7 // 将周日的0转换为7
    const startOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - dayOfWeek + 1).getTime()
    const endOfWeek = startOfWeek + 7 * 24 * 60 * 60 * 1000 - 1
    return this.getRecordsByDateRange(startOfWeek, endOfWeek)
  }

  // 获取本月记录
  getThisMonthRecords() {
    const today = new Date()
    const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1).getTime()
    const endOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0, 23, 59, 59, 999).getTime()
    return this.getRecordsByDateRange(startOfMonth, endOfMonth)
  }
}

export const timerRecordService = new TimerRecordService() 